package appengine.infrastructure.commons;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;

/**
 * 
 * @author m.sharif	(mosabsalih@gmail.com)
 *
 */

public final class HttpTool {

	private static final Logger log = Logger.getLogger(HttpTool.class.getSimpleName());

	public static HTTPResponse doGet(String url) {

		HTTPResponse httpResponse = null;

		try {

			URL theUrl = new URL(url);

			HTTPRequest httpRequest = new HTTPRequest(theUrl, HTTPMethod.GET);

			URLFetchService  fetchService = URLFetchServiceFactory.getURLFetchService();

			httpResponse = fetchService.fetch(httpRequest);
			

		} catch (MalformedURLException e) {

			log.severe("Error happend when doing an HTTP get .. " );
			log.log(Level.SEVERE, "MalformedURLException", e);

		} catch (IOException e) {

			log.severe("Error happend when doing an HTTP get .. " );
			log.log(Level.SEVERE, "IOException", e);

		} catch(Throwable t) {

			log.severe("Error happend when doing an HTTP get .. " );
			log.log(Level.SEVERE, "Throwable", t);

		}

		return httpResponse;
		
	}

	
	public static HTTPResponse doPost(String url, byte[] payload) {

		HTTPResponse httpResponse = null;

		try {

			URL theUrl = new URL(url);

			HTTPRequest httpRequest = new HTTPRequest(theUrl, HTTPMethod.POST);

			httpRequest.setPayload(payload);
			
			URLFetchService  fetchService = URLFetchServiceFactory.getURLFetchService();
					
			httpResponse = fetchService.fetch(httpRequest);
			

		} catch (MalformedURLException e) {

			log.severe("Error happend when doing an HTTP post .. " );
			log.log(Level.SEVERE, "MalformedURLException", e);

		} catch (IOException e) {

			log.severe("Error happend when doing an HTTP post .. " );
			log.log(Level.SEVERE, "IOException", e);

		} catch(Throwable t) {

			log.severe("Error happend when doing an HTTP post .. " );
			log.log(Level.SEVERE, "Throwable", t);

		}

		return httpResponse;
		
	}
	
	public static HTTPResponse doPut(String url, byte[] payload) {

		HTTPResponse httpResponse = null;

		try {

			URL theUrl = new URL(url);

			HTTPRequest httpRequest = new HTTPRequest(theUrl, HTTPMethod.PUT);

			httpRequest.setPayload(payload);
			
			URLFetchService  fetchService = URLFetchServiceFactory.getURLFetchService();
					
			httpResponse = fetchService.fetch(httpRequest);
			

		} catch (MalformedURLException e) {

			log.severe("Error happend when doing an HTTP put .. " );
			log.log(Level.SEVERE, "MalformedURLException", e);

		} catch (IOException e) {

			log.severe("Error happend when doing an HTTP put .. " );
			log.log(Level.SEVERE, "IOException", e);

		} catch(Throwable t) {

			log.severe("Error happend when doing an HTTP put .. " );
			log.log(Level.SEVERE, "Throwable", t);

		}

		return httpResponse;
		
	}

	public static HTTPResponse doDelete(String url) {

		HTTPResponse httpResponse = null;

		try {

			URL theUrl = new URL(url);

			HTTPRequest httpRequest = new HTTPRequest(theUrl, HTTPMethod.DELETE);
		
			URLFetchService  fetchService = URLFetchServiceFactory.getURLFetchService();
					
			httpResponse = fetchService.fetch(httpRequest);
			

		} catch (MalformedURLException e) {

			log.severe("Error happend when doing an HTTP delete .. " );
			log.log(Level.SEVERE, "MalformedURLException", e);

		} catch (IOException e) {

			log.severe("Error happend when doing an HTTP delete .. " );
			log.log(Level.SEVERE, "IOException", e);

		} catch(Throwable t) {

			log.severe("Error happend when doing an HTTP delete .. " );
			log.log(Level.SEVERE, "Throwable", t);

		}

		return httpResponse;
		
	}
	
	public static HTTPResponse doHead(String url) {

		HTTPResponse httpResponse = null;

		try {

			URL theUrl = new URL(url);

			HTTPRequest httpRequest = new HTTPRequest(theUrl, HTTPMethod.HEAD);
		
			URLFetchService  fetchService = URLFetchServiceFactory.getURLFetchService();
					
			httpResponse = fetchService.fetch(httpRequest);
			

		} catch (MalformedURLException e) {

			log.severe("Error happend when doing an HTTP head .. " );
			log.log(Level.SEVERE, "MalformedURLException", e);

		} catch (IOException e) {

			log.severe("Error happend when doing an HTTP head .. " );
			log.log(Level.SEVERE, "IOException", e);

		} catch(Throwable t) {

			log.severe("Error happend when doing an HTTP head .. " );
			log.log(Level.SEVERE, "Throwable", t);

		}

		return httpResponse;
		
	}
}
